package com.tomehay.termprojectmanage_server.controller;

import com.tomehay.termprojectmanage_server.pojo.Model.Code;
import com.tomehay.termprojectmanage_server.pojo.VO.ResultVO;
import com.tomehay.termprojectmanage_server.service.CodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import org.springframework.core.io.Resource;
import java.io.IOException;
import java.util.List;

/**
 * (Code)控制层
 *
 * @author makejava
 * @since 2023-06-24 15:54:08
 */
@RestController
@RequestMapping("/api/code")
public class CodeController {
    /**
     * 服务对象
     */
    @Autowired
    private CodeService codeService;

    /**
     * 通过主键查询单条数据
     *
     * @param code 参数对象
     * @return 单条数据
     *
     */
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public ResultVO<Code> selectOne(Code code) {
        Code result = codeService.selectById(code.getId());
        if(result != null){
            return ResultVO.createSuccessResponse(result);
        }
        return ResultVO.createErrorResponse();
    }
    
    /**
     * 新增一条数据
     *
     * @param code 实体类
     * @return Response对象
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public ResultVO<Code> insert(@RequestBody Code code, @RequestParam("projectId") Integer pid) {
        int result = codeService.insert(code, pid);
        if (result > 0) {
            return ResultVO.createSuccessResponse(code);
        }
        return ResultVO.createErrorResponse();
    }

    /**
     * 修改一条数据
     *
     * @param code 实体类
     * @return Response对象
     */
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public ResultVO<Code> update(@RequestBody Code code) {
        Code result = codeService.update(code);
        if (result != null) {
            return ResultVO.createSuccessResponse(result);
        }
        return ResultVO.createErrorResponse();
    }

    /**
     * 删除一条数据
     *
     * @param code 参数对象
     * @return Response对象
     */
    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    public ResultVO<Code> delete(Code code) {
        int result = codeService.deleteById(code.getId());
        if (result > 0) {
            return ResultVO.createSuccessResponse(null);
        }
        return ResultVO.createErrorResponse();
    }

    /**
     * 查询全部
     *
     * @return Response对象
     */
    @RequestMapping(value = "selectAll", method = RequestMethod.GET)
    public ResultVO<List<Code>> selectAll() {
        List<Code> codes = codeService.selectAll();
        if (codes != null) {
            return ResultVO.createSuccessResponse(codes);
        }
        return ResultVO.createErrorResponse();
    }

    /**
     * 分页查询
     *
     * @param start 偏移
     * @param limit 条数
     * @return Response对象
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.GET)
    public ResultVO<List<Code>> selectPage(Integer start, Integer limit) {
        List<Code> codes = codeService.selectPage(start, limit);
        if (codes != null) {
            return ResultVO.createSuccessResponse(codes);
        }
        return ResultVO.createErrorResponse();
    }

    @PostMapping("/uploadCodeBatch")
    public void uploadCodeBatch(@RequestParam("projectId") Integer pid, @RequestParam("gmno") Integer gmno, @RequestParam("gno") Integer gno, @RequestPart("fileList") List<MultipartFile> files) {
        codeService.uploadCodeBatch(pid, gmno, gno, files);
    }

    @PostMapping("/uploadCode")
    public void uploadCode(@RequestParam("no") String no, @RequestParam("description") String description, @RequestParam("projectId") Integer pid, @RequestParam("gmno") Integer gmno, @RequestParam("gno") Integer gno,@RequestPart("file") MultipartFile file) {
        codeService.uploadCode(no, description,pid, gmno, gno, file);
    }


    @GetMapping("/download")
    @ResponseBody
    public ResponseEntity<InputStreamResource> download(@RequestParam(value = "id") Integer id) {
        return codeService.download(id);
    }

    @GetMapping("/listByProjectId")
    public ResultVO<List<Code>> listByProjectId(@RequestParam(value = "projectId") Integer projectId) {
        List<Code> codes = codeService.listByProjectId(projectId);
        if (codes != null) {
            return ResultVO.createSuccessResponse(codes);
        }
        return ResultVO.createErrorResponse();
    }

    @GetMapping("/getDetail")
    public ResponseEntity<Resource> getDetail(@RequestParam("codeId") Integer cid) {
        return codeService.getDetail(cid);
    }

    @PostMapping("/updateCode")
    public ResultVO<Integer> updateCode(@RequestParam("id") Integer cid, @RequestPart("file") MultipartFile file) {
        Integer integer = codeService.updateCode(cid, file);
        return ResultVO.createSuccessResponse(integer);
    }

}

